package com.coupon.constrol;

import com.coupon.mapper.ManagerMapper;
import com.coupon.pojo.Manager;
import com.coupon.service.ManagerService;
import com.coupon.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author :wn
 * @program : coupon
 * @descript :
 * @create :2021-01-07 09:05
 */
@Controller
@ResponseBody
@RequestMapping("/mgr")

public class ManagerController {

    @Resource
    ManagerService managerService;

    @Resource
    ManagerMapper managerMapper;

    /**4、工作人员登录信息Controller方法*/
    @PostMapping("/login")//localhost:8081/mapper/login
    public Map login(@RequestParam( "phone")String phone) {

        Manager login = managerService.login(phone);

        //声明JsonResult实例
        JsonResult jsonResult=new JsonResult();
        //把结果集放进去，就会直接返回给客户端json对象，不用我们手动实现序列化接口再处理

        if (login!=null){
            jsonResult.setCode(1);
            jsonResult.setData(login);
        }else{
            jsonResult.setCode(0);
        }
        return jsonResult.getValues();
    }
    /**6、获取工作人员列表信息Controller方法*/
    @RequestMapping("/getList")
    public Map getList(@RequestParam("page") Integer page, @RequestParam( value = "name",required = false) String name ){

        Map all = managerService.findAll(page,name);
       /* JsonResult jsonResult=new JsonResult();
        jsonResult.setCode(1);
        jsonResult.setData(all);*/
        all.put("code",1);
        return all;
    }


    /**5、新增工作人员信息Controller方法*/
    @RequestMapping("/add")
    public Map<String, Object> add(@RequestBody Manager manager){

        Date date=new Date();
        manager.setRegTime(date);

        Boolean insert=managerService.registerManage(manager);
        JsonResult jsonResult = new JsonResult();
        if (insert)
        {
            jsonResult.setCode(1);
            System.out.println("添加成功！");
        }
        else
        {
            jsonResult.setCode(0);
            System.out.println("添加失败，该手机号已经存在！");
        }
        return jsonResult.getValues();
    }


    /**7、修改工作人员信息控制方法*/
    @RequestMapping("/update")
    public Map<String,Object> update(@RequestParam("phone")String phone,@RequestParam("use")Integer use){

        Manager manager=new Manager();
        manager.setPhone(phone);
        Boolean b= (Boolean) Boolean.parseBoolean(String.valueOf(use));
        manager.setInUse(b);
        Boolean update=managerService.updateManage(manager);
        JsonResult jsonResult = new JsonResult();
        if (update)
        {
            //如果修改成功
            System.out.println("修改成功！");
            jsonResult.setCode(1);
        }
        else
        {
            //如果修改失败
            System.out.println("修改失败！");
            jsonResult.setCode(0);
        }
        return jsonResult.getValues();
    }



    /**8、删除工作人员信息控制方法*/
    @RequestMapping("/delete")
    public Map<String,Object> delete(@RequestParam("phone")String phone){
        Boolean delete=managerService.deleteManage(phone);
        JsonResult jsonResult = new JsonResult();
        if (delete)
        {
            //如果删除成功
            System.out.println("删除成功！");
            jsonResult.setCode(1);
        }
        else
        {
            //如果删除失败
            System.out.println("删除失败！");
            jsonResult.setCode(0);
        }
        return jsonResult.getValues();
    }




}
